package com.basics.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.basics.api.request.organization.*;
import com.basics.api.response.organization.*;
import com.basics.api.service.OrganizationApiService;
import com.basics.api.service.UserApiService;
import com.basics.api.vo.UserInfo;
import com.basics.channel.entity.CyChannel;
import com.basics.channel.service.CyChannelService;
import com.basics.common.utils.AsyncUtil;
import com.basics.framework.web.domain.PageBaseEntity;
import com.basics.framework.web.domain.RestResult;
import com.basics.organization.entity.CyOrganization;
import com.basics.organization.entity.CyOrganizationUser;
import com.basics.organization.service.CyOrganizationService;
import com.basics.organization.service.CyOrganizationUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jodd.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/organization/")
@Api(tags = "组织相关")
public class OrganizationApiController extends BaseApiController{

    @Autowired
    private CyOrganizationUserService cyOrganizationUserService;
    @Autowired
    private OrganizationApiService organizationApiService;
    @Autowired
    private CyOrganizationService cyOrganizationService;

    @Autowired
    private CyChannelService cyChannelService;

    @Autowired
    private UserApiService userApiService;

    @GetMapping("/selectJoinOrganizationList")
    @ApiOperation("选择组织/左边侧栏-获取用户加入的组织列表")
    public RestResult<List<OrganizationListData>> selectJoinOrganizationList(){
        return RestResult.success(cyOrganizationUserService.selectJoinOrganizationList(getCurrentUser().getUserId()));
    }

    @GetMapping("/checkOrganization")
    @ApiOperation("选择组织/左边侧栏-选择组织进入")
    public RestResult<CheckNeedEditInfoData> checkOrganization(@RequestParam(value = "organizationId")Long organizationId){
        cyOrganizationUserService.checkOrganization(organizationId,getCurrentUser().getUserId());
        UserInfo info = userApiService.getUserInfoData(getCurrentUser().getUserId());
        CheckNeedEditInfoData data = new CheckNeedEditInfoData();
        data.setNeedEdit(StringUtil.isEmpty(info.getAvatar()));
        Long userId = getCurrentUser().getUserId();
        AsyncUtil.runAsync(()->{
            organizationApiService.pushChannelMsg(organizationId,userId);
        });
        return RestResult.success(data);
    }

    @GetMapping("/getOrganizationInfo")
    @ApiOperation("右边更多-获取组织详情信息")
    public RestResult<OrganizationInfoData> getOrganizationInfo(){
        return RestResult.success(organizationApiService.getOrganizationInfo(getOrganizationId(),getCurrentUser().getUserId()));
    }

    @GetMapping("/getInvitionUrl")
    @ApiOperation("右边更多-邀请好友链接")
    public RestResult<InvitionUrlData> getInvitionUrl(){
        return RestResult.success(organizationApiService.getInvitionUrl(getOrganizationId(),getCurrentUser().getUserId()));
    }

    @GetMapping("/selectGroupOrganizationMemberList")
    @ApiOperation("分页获取组织成员列表")
    public RestResult<IPage<GroupOrganizationMemberListData>> selectGroupOrganizationMemberList(GroupOrganizationMemberReq req){
        return RestResult.success(organizationApiService.selectGroupOrganizationMemberList(req,getCurrentUser().getUserId()));
    }

    @GetMapping("/getGroupOrganization")
    @ApiOperation("组织分组-获取组织分组数据")
    public RestResult<GroupOrganizationData> getGroupOrganization(){
        return RestResult.success(organizationApiService.getGroupOrganization(getOrganizationId(),getCurrentUser().getUserId()));
    }

    @PostMapping("/saveGroupOrganization")
    @ApiOperation("添加/修改分组-新增/修改组织分组数据")
    public RestResult<String> saveGroupOrganization(@RequestBody EditOrganizationReq req){
        organizationApiService.saveGroupOrganization(req,getCurrentUser().getUserId(),getOrganizationId());
        return RestResult.success();
    }

    @GetMapping("/getEditOrganizationGroupInfo")
    @ApiOperation("添加/修改分组-获取编辑组织分组数据")
    public RestResult<EditOrganizationReq> getEditOrganizationGroupInfo(@RequestParam(value = "groupId")String groupId){
        return RestResult.success(organizationApiService.getEditOrganizationGroupInfo(groupId,getCurrentUser().getUserId()));
    }

    @GetMapping("/deleteGroupOrganization")
    @ApiOperation("添加/修改分组-删除组织分组数据")
    public RestResult<String> deleteGroupOrganization(@RequestParam(value = "groupId")String groupId){
        organizationApiService.deleteGroupOrganization(groupId,getCurrentUser().getUserId());
        return RestResult.success();
    }

    @GetMapping("/selectJoinOrganizationMemberList")
    @ApiOperation("添加/修改分组-获取分组下面成员列表")
    public RestResult<IPage<OrganizationMemberListData>> selectJoinOrganizationMemberList(GroupReq req){
        return RestResult.success(cyOrganizationUserService.selectJoinOrganizationMemberList(new Page(req.getPageNum()
                ,req.getPageSize()),getOrganizationId(),req.getGroupId()));
    }

    @GetMapping("/removeGroupOrganization")
    @ApiOperation("添加/修改分组-移除用户分组信息")
    public RestResult<String> removeGroupOrganization(OperateGroupReq req){
        organizationApiService.removeGroupOrganization(req,getCurrentUser().getUserId());
        return RestResult.success();
    }

    @PostMapping("/addGroupOrganization")
    @ApiOperation("添加/修改分组-添加用户到分组")
    public RestResult<String> addGroupOrganization(@RequestBody OperateGroupReq req){
        organizationApiService.addGroupOrganization(req,getCurrentUser().getUserId(),getOrganizationId());
        return RestResult.success();
    }

    @GetMapping("/selectChannelList")
    @ApiOperation("应用管理-频道管理列表")
    public RestResult<IPage<CyChannel>> selectChannelList(PageBaseEntity req){
        return RestResult.success( cyChannelService.page(new Page<>(req.getPageNum()
                ,req.getPageSize()),new QueryWrapper<CyChannel>()
                .eq("organization_id",getOrganizationId()).eq("del_flag",0)
                .orderByDesc("create_time")));
    }

    @GetMapping("/getChannelInfo")
    @ApiOperation("添加或删除频道-频道信息(用于编辑)")
    public RestResult<CyChannel> getChannelInfo(@RequestParam(value = "channelId")Long channelId){
        return RestResult.success(cyChannelService.getChannelInfo(channelId,getOrganizationId()));
    }

    @PostMapping("/saveChannel")
    @ApiOperation("添加或删除频道-保存频道信息")
    public RestResult<String> saveChannel(@RequestBody CyChannel cyChannel){
        organizationApiService.saveChannel(cyChannel,getCurrentUser().getUserId(),getOrganizationId());
        return RestResult.success();
    }

    @GetMapping("/deleteChannel")
    @ApiOperation("添加或删除频道-删除频道")
    public RestResult<String> deleteChannel(@RequestParam(value = "channelId")Long channelId){
        organizationApiService.deleteChannel(channelId,getCurrentUser().getUserId(),getOrganizationId());
        return RestResult.success();
    }


    @PostMapping("/joinOrganizationByPhone")
    @ApiOperation("通过手机号加入组织(h5网页)")
    private RestResult<String> joinOrganizationByPhone(@RequestBody JoinOrganiztionByPhoneReq req){
        organizationApiService.joinOrganizationByPhone(req);
        return RestResult.success();
    }

    @GetMapping("/getOrganizationForH5")
    @ApiOperation("获取组织信息(h5网页)")
    private RestResult<CyOrganization> getOrganizationForH5(@RequestParam(value = "organizationId")Long organizationId){
        return RestResult.success(cyOrganizationService.getById(organizationId));
    }

}
